package osp.Resources;

import osp.IFLModules.*;
import osp.Threads.*;

/**
   The studends module for dealing with resource management. The methods 
   that have to be implemented are do_grant().

   @OSPProject Resources
*/

public class RRB extends IflRRB
{
    /** 
        constructor of class RRB 
        Creates a new RRB object. This constructor must have
        super() as its first statement.

        @OSPProject Resources
    */   
    public RRB(ThreadCB thread, ResourceCB resource,int quantity)
    {
        super(thread, resource, quantity);// your code goes here

    }

    /**
       This method is called when we decide to grant an RRB.
       The method must update the various resource quantities
       and notify the thread waiting on the granted RRB.

        @OSPProject Resources
    */
    public void do_grant()
    {
        /*
         * decrements the number of
         * available instances of the requested resource by the requested quantity and
         * increments the number of allocated instances of this resource by that same
         * quantity.
         * The current number of available instances of a resource is given
    	 * by the method getAvailable() and is set by the method setAvailable().
    	 * Similarly, the number of allocated resources is obtained and changed using
    	 * the methods getAllocated() and setAllocated(), respectively
    	 */
    	 
    	ResourceCB resource = this.getResource();
    	ThreadCB thread = this.getThread();
    	 
    	int qttAvailable = resource.getAvailable();
    	int qttAlocatted = resource.getAllocated(thread);
    	 
    	resource.setAvailable(qttAvailable-this.getQuantity());
    	resource.setAllocated(thread, qttAlocatted+this.getQuantity());
    	
    	/*
    	 * To finish granting the request, the status of the RRB must be set to Granted
    	 * and the thread that was waiting on this RRB should be resumed. The latter
    	 * is done by invoking the method notifyThreads() of class Event (recall that
    	 * a RRB is also an Event object).
    	 */
    	
    	this.setStatus(Granted);
    	this.notifyThreads();

    }


    /*
       Feel free to add methods/fields to improve the readability of your code
    */

}

/*
      Feel free to add local classes to improve the readability of your code
*/
